package com.evenmo.gxlcTest;

import com.evenmo.pojo.cj.CJ2;
import com.evenmo.pojo.cj.Cj;
import com.evenmo.pojo.cj.CjId;
import com.evenmo.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.junit.Test;

import java.util.List;

public class CJTst {
    @Test
    public void  saveCj(){
        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();//开启事务
        CjId id =new CjId();
        id.setXh("1111");
        id.setKch("1111");

        Cj cj=new Cj();
        cj.setId(id);
        cj.setCj(80);

        session.save(cj);

        transaction.commit();
        session.close();

    }

    @Test
    public void  updateCj(){
        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();//开启事务
        CjId id =new CjId();
        id.setXh("12332");
        id.setKch("001");

        Cj cj= session.load(Cj.class, id);
        //主键、复合主键不可修改
        //修改成绩
        cj.setCj(100);

        session.update(cj);

        transaction.commit();
        session.close();

    }

    @Test
    public void  saveCj2(){
        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();//开启事务
        CJ2 cj2=new CJ2();
        cj2.setXh("12311");
        cj2.setKch("0003");
        cj2.setCj(111);

        session.save(cj2);

        transaction.commit();
        session.close();

    }

    @Test
    public void  updateCj2(){
        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();//开启事务
        CJ2 cj2 =new CJ2();
        cj2.setXh("12333");
        cj2.setKch("002");

        Query<CJ2> query = session.createQuery("from CJ2 where xh='12333' and kch='002'");
        /*
        主键、复合主键不可修改
        修改成绩
        */
        List<CJ2> cj2s = query.list();

        cj2=cj2s.get(0);
        cj2.setCj(99);

        session.update(cj2);

        transaction.commit();
        session.close();

    }
}
